#pragma once
#include <BWAPI.h>
#include <BWTA.h>
#include <Arbitrator.h>
#include <WorkerManager.h>
#include <SupplyManager.h>
#include <BuildManager.h>
#include <BuildOrderManager.h>
#include <TechManager.h>
#include <UpgradeManager.h>
#include <BaseManager.h>
#include <ScoutManager.h>
#include <DefenseManager.h>
#include <InformationManager.h>
#include <BorderManager.h>
#include <UnitGroupManager.h>
#include <EnhancedUI.h>
#include <windows.h>
#include "EngageManager.h"
#include "AttackManager.h"


// debug!
#include <dbghelp.h>
#include <tchar.h>

BOOL
WINAPI
MiniDumpWriteDump(
    IN HANDLE hProcess,
    IN DWORD ProcessId,
    IN HANDLE hFile,
    IN MINIDUMP_TYPE DumpType,
    IN CONST PMINIDUMP_EXCEPTION_INFORMATION ExceptionParam, OPTIONAL
    IN CONST PMINIDUMP_USER_STREAM_INFORMATION UserStreamParam, OPTIONAL
    IN CONST PMINIDUMP_CALLBACK_INFORMATION CallbackParam OPTIONAL
    );


void CreateMiniDump( EXCEPTION_POINTERS* pep ) ;

/////
DWORD WINAPI AnalyzeThread(LPVOID param);

class BasicAIModule : public BWAPI::AIModule
{
  public:
    virtual void onStart();
    virtual void onEnd(bool isWinner);
    virtual void onFrame();
    virtual void onUnitDiscover(BWAPI::Unit* unit);
    virtual void onUnitEvade(BWAPI::Unit* unit);
    virtual void onUnitMorph(BWAPI::Unit* unit);
    virtual void onUnitRenegade(BWAPI::Unit* unit);
    virtual void onUnitDestroy(BWAPI::Unit* unit);
    virtual void onSendText(std::string text);
    ~BasicAIModule(); //not part of BWAPI::AIModule
    void showStats(); //not part of BWAPI::AIModule
    void showPlayers();
    void showForces();

    // Custom event handlers
    void onExpand(BWTA::BaseLocation* target);

    static bool analyzed;
    __in HANDLE analyzeThreadHandle;
    std::map<BWAPI::Unit*,BWAPI::UnitType> buildings;
    Arbitrator::Arbitrator<BWAPI::Unit*,double> arbitrator;
    WorkerManager* workerManager;
    SupplyManager* supplyManager;
    BuildManager* buildManager;
    TechManager* techManager;
    UpgradeManager* upgradeManager;
    BaseManager* baseManager;
    ScoutManager* scoutManager;
    BuildOrderManager* buildOrderManager;
    AttackManager* attackManager;
    DefenseManager* defenseManager;
    EngageManager* engageManager;
    InformationManager* informationManager;
    BorderManager* borderManager;
    UnitGroupManager* unitGroupManager;
    EnhancedUI* enhancedUI;
    bool showManagerAssignments;

    // Expansion Logic
    bool firstExpand; // can be set false to delay the first expansion
    int lastExpand; // most recent expand frame
    std::list<std::pair<BWTA::BaseLocation*, double> > bases; // list of bases and distance from home
    void evaluateExpand();
    int larvaThreshold;

    // Timer monitoring
    std::map<int, int> timeMonitor;
    std::map<int, std::map<int, int> > moduleMonitor;
    void reportTime(int id, int time);
};
